package net.sudot.chess.business.dao;

import net.sudot.chess.business.model.Room;
import net.sudot.commons.pagination.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.TypedQuery;
import java.util.List;

/**
 * 房间
 *
 * @author tangjialin on 2018-03-03.
 */
@Repository
public class RoomDao extends BaseDao<Room> {

    /**
     * 获取等待中的房间信息
     *
     * @param gameId 游戏主键
     * @return 返回房间信息
     */
    public Room findWaitByGame(Long gameId) {
        String ql = "SELECT room FROM Room room WHERE room.gameId = :gameId AND room.status = :status";
        TypedQuery<Room> query = entityManager.createQuery(ql, entityClass);
        query.setParameter("gameId", gameId);
        query.setParameter("status", Room.Status.WAIT);
        return uniqueResult(query);
    }

    /**
     * 获取游戏中的房间信息
     *
     * @param wechatGroupId 微信群主键
     * @return 返回房间信息
     */
    public Room findPlayingByWechatGroup(Long wechatGroupId) {
        String ql = "SELECT room FROM Room room WHERE room.wechatGroupId = :wechatGroupId AND room.status = :status";
        TypedQuery<Room> query = entityManager.createQuery(ql, entityClass);
        query.setParameter("wechatGroupId", wechatGroupId);
        query.setParameter("status", Room.Status.PLAYING);
        return uniqueResult(query);
    }

    /**
     * 获取未关闭的房间
     *
     * @param customerId 客户主键
     * @return 返回客户正在参与的房间
     */
    public List<Room> findListOpenByCustomer(Long customerId) {
        String ql = "SELECT room" +
                " FROM RoomCustomerRef roomCustomerRef" +
                " INNER JOIN Room room ON room.id = roomCustomerRef.roomId" +
                " WHERE roomCustomerRef.customerId = :customerId" +
                " AND room.status IN (:statusWait,:statusPlaying)" +
                " ORDER BY room.createdDate DESC";
        TypedQuery<Room> query = entityManager.createQuery(ql, entityClass);
        query.setParameter("customerId", customerId);
        query.setParameter("statusWait", Room.Status.WAIT);
        query.setParameter("statusPlaying", Room.Status.PLAYING);
        return query.getResultList();
    }

    /**
     * 获取已完成的房间信息
     *
     * @param pageable   分页参数
     * @param customerId 客户主键
     * @return 返回已完成的房间信息
     */
    public List<Room> findListCompleteByCustomer(Pageable pageable, Long customerId) {
        String ql = "SELECT room" +
                " FROM RoomCustomerRef roomCustomerRef" +
                " INNER JOIN Room room ON room.id = roomCustomerRef.roomId" +
                " WHERE roomCustomerRef.customerId = :customerId" +
                " AND room.status IN (:endStatus,:cancelStatus)";
        TypedQuery<Room> query = entityManager.createQuery(ql, entityClass);
        query.setParameter("customerId", customerId);
        query.setParameter("endStatus", Room.Status.END);
        query.setParameter("cancelStatus", Room.Status.CANCEL);
        settingPagination(query, pageable);
        return query.getResultList();
    }

}
